home *** CD-ROM | disk | FTP | other *** search
- package com.ibm.xml.xpointer;
-
- import com.ibm.xml.parser.AttDef;
- import com.ibm.xml.parser.Child;
- import com.ibm.xml.parser.DTD;
- import com.ibm.xml.parser.LibraryException;
- import com.ibm.xml.parser.Parent;
- import com.ibm.xml.parser.TXDocument;
- import com.ibm.xml.parser.TXElement;
- import java.io.Serializable;
- import java.util.Enumeration;
- import java.util.Vector;
- import org.w3c.dom.Attr;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
-
- public class XPointer implements Serializable {
- static final long serialVersionUID = 5186377505549567985L;
- public static final String S_ALL = "all";
- public static final String S_END = "end";
- public static final int NT_NONE = 0;
- public static final int NT_NAME = 1;
- public static final int NT_ELEMENT = 2;
- public static final int NT_PI = 3;
- public static final int NT_COMMENT = 4;
- public static final int NT_TEXT = 5;
- public static final int NT_CDATA = 6;
- public static final int NT_ALL = 7;
- public static final String[] nodetypes = new String[]{null, null, "#element", "#pi", "#comment", "#text", "#cdata", "#all"};
- public static final String S_ELEMENT = "element";
- public static final String S_PI = "pi";
- public static final String S_COMMENT = "comment";
- public static final String S_TEXT = "text";
- public static final String S_CDATA = "cdata";
- public static final String S_NIMPLIED = "#IMPLIED";
- public static final String S_IMPLIED = "IMPLIED";
- public static final String[] literals = new String[]{null, "root", "origin", "id", "html", "child", "descendant", "ancestor", "preceding", "psibling", "following", "fsibling", "span", "attr", "string"};
- public static final int ST_NONE = -1;
- public static final int ST_ROOT = 1;
- public static final int ST_ORIGIN = 2;
- public static final int ST_ID = 3;
- public static final int ST_HTML = 4;
- public static final int ST_CHILD = 5;
- public static final int ST_DESCENDANT = 6;
- public static final int ST_ANCESTOR = 7;
- public static final int ST_PRECEDING = 8;
- public static final int ST_PSIBLING = 9;
- public static final int ST_FOLLOWING = 10;
- public static final int ST_FSIBLING = 11;
- public static final int ST_SPAN = 12;
- public static final int ST_ATTR = 13;
- public static final int ST_STRING = 14;
- public static final int T_IMPLIED = 0;
- public static final int T_ANY = 1;
- public static final int T_NAME = 2;
- public static final int T_EXACT = 3;
- AbsTerm absTerm;
- Vector otherTerms;
- String string;
- private transient int previous = -1;
-
- public XPointer(AbsTerm var1, Vector var2) {
- this.absTerm = var1;
- this.otherTerms = var2 == null ? new Vector() : var2;
- }
-
- public AbsTerm getAbsTerm() {
- return this.absTerm;
- }
-
- public void setAbsTerm(AbsTerm var1) {
- this.absTerm = var1;
- this.string = null;
- }
-
- public Vector getOtherTermsVector() {
- return this.otherTerms;
- }
-
- public void appendOtherTerm(OtherTerm var1) {
- if (this.otherTerms == null) {
- this.otherTerms = new Vector();
- }
-
- this.otherTerms.addElement(var1);
- this.string = null;
- }
-
- public void insertOtherTerm(OtherTerm var1) {
- if (this.otherTerms == null) {
- this.otherTerms = new Vector();
- }
-
- this.otherTerms.insertElementAt(var1, 0);
- this.string = null;
- }
-
- public OtherTerm removeLastOtherTerm() {
- OtherTerm var1 = null;
- Vector var2 = this.otherTerms;
- synchronized(var2){}
-
- try {
- int var4 = this.otherTerms.size();
- if (var4 > 0) {
- var1 = (OtherTerm)this.otherTerms.elementAt(var4 - 1);
- this.otherTerms.removeElementAt(var4 - 1);
- }
- } catch (Throwable var6) {
- throw var6;
- }
-
- this.string = null;
- return var1;
- }
-
- public String toString() {
- if (this.string != null) {
- return this.string;
- } else {
- StringBuffer var1 = new StringBuffer(100);
- if (this.absTerm != null) {
- var1.append(this.absTerm.toString());
- if (this.otherTerms.size() > 0) {
- var1.append(".");
- }
- }
-
- int var2 = 0;
- Enumeration var3 = this.otherTerms.elements();
-
- while(var3.hasMoreElements()) {
- if (var2 > 0) {
- var1.append(".");
- }
-
- ++var2;
- var1.append(var3.nextElement().toString());
- }
-
- return this.string = var1.toString();
- }
- }
-
- public synchronized Pointed point(Document var1) {
- Element var2 = var1.getDocumentElement();
- if (this.absTerm != null && this.absTerm.getType() != 1) {
- if (this.absTerm.getType() == 2 || this.absTerm.getType() == 4) {
- return new Pointed();
- }
-
- DTD var3 = ((TXDocument)var1).getDTD();
- if (var3 == null) {
- return new Pointed();
- }
-
- var2 = var3.checkID(this.absTerm.getParameter());
- if (var2 == null) {
- return new Pointed();
- }
- }
-
- return this.point((Node)var2);
- }
-
- public synchronized Pointed point(Node var1) {
- Pointed var2 = new Pointed();
- var2.add(var1);
- this.previous = -1;
-
- Pointed var5;
- for(Enumeration var3 = this.otherTerms.elements(); var3.hasMoreElements(); var2 = var5) {
- Object var4 = var3.nextElement();
- var5 = new Pointed();
- Enumeration var6 = ((Vector)var2).elements();
-
- while(var6.hasMoreElements()) {
- Pointed.Item var7 = (Pointed.Item)var6.nextElement();
- if (var7.node != null) {
- this.point(var5, var4, (Child)var7.node);
- }
- }
- }
-
- return var2;
- }
-
- public static XPointer makeXPointer(Child var0) {
- if (var0.getNodeType() == 9) {
- return null;
- } else if (var0.getNodeType() == 5) {
- return null;
- } else {
- Child var1 = var0;
-
- while(var1.getNodeType() != 9) {
- var1 = (Child)var1.getParentNode();
- if (var1 == null) {
- return null;
- }
- }
-
- TXDocument var2 = (TXDocument)var1;
- if (var0.getParentNode() == var2) {
- return new XPointer(new AbsTerm(1, (String)null), (Vector)null);
- } else {
- DTD var3 = var2.getDTD();
- if (var3 != null) {
- Child var4 = var0.getNodeType() != 1 ? (Child)var0.getParentWithoutReference() : var0;
-
- do {
- TXElement var5 = (TXElement)var4;
- Enumeration var6 = var5.attributeElements();
-
- while(var6.hasMoreElements()) {
- Attr var7 = (Attr)var6.nextElement();
- AttDef var8 = var3.getAttributeDeclaration(var5.getNodeName(), var7.getName());
- if (var8 != null && var8.getDeclaredType() == 2) {
- XPointer var9 = new XPointer(new AbsTerm(3, var7.getValue()), (Vector)null);
- if (var5 != var0) {
- var1 = var0;
- Object var10 = null;
-
- do {
- var9.insertOtherTerm(var17 = makeRelTerm(var1));
- var1 = (Child)var1.getParentWithoutReference();
- } while(var1 != var5);
-
- var17.setType(5);
- }
-
- return var9;
- }
- }
-
- var4 = (Child)var4.getParentWithoutReference();
- } while(!(var4 instanceof TXDocument));
- }
-
- TXElement var13 = (TXElement)var2.getDocumentElement();
- XPointer var14 = new XPointer(new AbsTerm(1, (String)null), (Vector)null);
- var1 = var0;
- Object var15 = null;
-
- do {
- var14.insertOtherTerm(var16 = makeRelTerm(var1));
- var1 = (Child)var1.getParentWithoutReference();
- } while(var1 != var13);
-
- var16.setType(5);
- return var14;
- }
- }
- }
-
- private void point(Pointed var1, Object var2, Child var3) {
- if (!(var2 instanceof RelTerm)) {
- if (var2 instanceof StringTerm) {
- this.previous = -1;
- } else if (var2 instanceof SpanTerm) {
- this.previous = -1;
- } else if (var2 instanceof AttrTerm) {
- this.previous = -1;
- if (var3 instanceof Element) {
- Attr var10 = ((Element)var3).getAttributeNode(((AttrTerm)var2).getName());
- if (var10 != null) {
- var1.add(var10.getValue());
- return;
- }
- }
-
- } else {
- throw new LibraryException("com.ibm.xml.xpointer.XPointer#point(): Internal Error: unknown OtherTerm.");
- }
- } else {
- RelTerm var4 = (RelTerm)var2;
- int var5 = var4.getType();
- if (var5 == -1) {
- if (this.previous == -1) {
- return;
- }
-
- var5 = this.previous;
- }
-
- int var6 = var4.getInstance();
- if (var6 != 0) {
- label299:
- switch (var5) {
- case 5:
- if (var4.isAll()) {
- for(Child var17 = (Child)var3.getFirstWithoutReference(); var17 != null; var17 = (Child)var17.getNextWithoutReference()) {
- if (var4.match(var17)) {
- var1.add(var17);
- }
- }
- } else if (var6 > 0) {
- for(Child var18 = (Child)var3.getFirstWithoutReference(); var18 != null; var18 = (Child)var18.getNextWithoutReference()) {
- if (var4.match(var18)) {
- --var6;
- if (var6 == 0) {
- var1.add(var18);
- break label299;
- }
- }
- }
- } else {
- for(Child var19 = (Child)var3.getLastWithoutReference(); var19 != null; var19 = (Child)var19.getPreviousWithoutReference()) {
- if (var4.match(var19)) {
- ++var6;
- if (var6 == 0) {
- var1.add(var19);
- break label299;
- }
- }
- }
- }
- break;
- case 6:
- if (!(var3 instanceof Element)) {
- return;
- }
-
- Vector var15 = new Vector();
- addMatchingNodes(var15, (Element)var3, var4);
- if (var4.isAll()) {
- for(int var22 = 0; var22 < var15.size(); ++var22) {
- var1.add((Node)var15.elementAt(var22));
- }
- } else if (var6 > 0) {
- if (var6 <= var15.size()) {
- var1.add((Node)var15.elementAt(var6 - 1));
- }
- } else if (var6 + var15.size() >= 0) {
- var1.add((Node)var15.elementAt(var6 + var15.size()));
- }
- break;
- case 7:
- Vector var14 = new Vector();
-
- while(var3.getParentWithoutReference() instanceof Element) {
- var3 = (Child)var3.getParentWithoutReference();
- if (var4.match(var3)) {
- var14.addElement(var3);
- }
- }
-
- if (var4.isAll()) {
- for(int var21 = 0; var21 < var14.size(); ++var21) {
- var1.add((Node)var14.elementAt(var21));
- }
- } else if (var6 > 0) {
- if (var6 <= var14.size()) {
- var1.add((Node)var14.elementAt(var6 - 1));
- }
- } else if (var6 + var14.size() >= 0) {
- var1.add((Node)var14.elementAt(var6 + var14.size()));
- }
- break;
- case 8:
- Vector var13 = new Vector();
-
- while(var3.getPreviousWithoutReference() != null || var3.getParentWithoutReference() instanceof Element) {
- if (var3.getPreviousWithoutReference() == null) {
- var3 = (Child)var3.getParentWithoutReference();
- } else {
- var3 = (Child)var3.getPreviousWithoutReference();
- }
-
- if (var4.match(var3)) {
- var13.addElement(var3);
- }
- }
-
- if (var4.isAll()) {
- for(int var20 = 0; var20 < var13.size(); ++var20) {
- var1.add((Node)var13.elementAt(var20));
- }
- } else if (var6 > 0) {
- if (var6 <= var13.size()) {
- var1.add((Node)var13.elementAt(var6 - 1));
- }
- } else if (var6 + var13.size() >= 0) {
- var1.add((Node)var13.elementAt(var6 + var13.size()));
- }
- break;
- case 9:
- if (var4.isAll()) {
- while((var3 = (Child)var3.getPreviousWithoutReference()) != null) {
- if (var4.match(var3)) {
- var1.add(var3);
- }
- }
- } else if (var6 > 0) {
- while((var3 = (Child)var3.getPreviousWithoutReference()) != null) {
- if (var4.match(var3)) {
- --var6;
- if (var6 == 0) {
- var1.add(var3);
- break label299;
- }
- }
- }
- } else {
- var6 = Math.abs(var6);
-
- for(Child var16 = (Child)((Child)var3.getParentWithoutReference()).getFirstWithoutReference(); var16 != null; var16 = (Child)var16.getNextWithoutReference()) {
- if (var4.match(var16)) {
- ++var6;
- if (var6 == 0) {
- var1.add(var16);
- break label299;
- }
- }
- }
- }
- break;
- case 10:
- Vector var7 = new Vector();
-
- while(var3.getNextWithoutReference() != null || var3.getParentWithoutReference() instanceof Element) {
- if (var3.getNextWithoutReference() == null) {
- var3 = (Child)var3.getParentWithoutReference();
- } else {
- var3 = (Child)var3.getNextWithoutReference();
- }
-
- if (var4.match(var3)) {
- var7.addElement(var3);
- }
- }
-
- if (var4.isAll()) {
- for(int var9 = 0; var9 < var7.size(); ++var9) {
- var1.add((Node)var7.elementAt(var9));
- }
- } else if (var6 > 0) {
- if (var6 <= var7.size()) {
- var1.add((Node)var7.elementAt(var6 - 1));
- }
- } else if (var6 + var7.size() >= 0) {
- var1.add((Node)var7.elementAt(var6 + var7.size()));
- }
- break;
- case 11:
- if (var4.isAll()) {
- while((var3 = (Child)var3.getNextWithoutReference()) != null) {
- if (var4.match(var3)) {
- var1.add(var3);
- }
- }
- } else if (var6 > 0) {
- while((var3 = (Child)var3.getNextWithoutReference()) != null) {
- if (var4.match(var3)) {
- --var6;
- if (var6 == 0) {
- var1.add(var3);
- break;
- }
- }
- }
- } else {
- var6 = Math.abs(var6);
-
- for(Child var8 = (Child)((Child)var3.getParentWithoutReference()).getLastWithoutReference(); var8 != null; var8 = (Child)var8.getPreviousWithoutReference()) {
- if (var4.match(var8)) {
- ++var6;
- if (var6 == 0) {
- var1.add(var8);
- break;
- }
- }
- }
- }
- }
-
- this.previous = var5;
- }
- }
- }
-
- private static void addMatchingNodes(Vector var0, Element var1, RelTerm var2) {
- for(Node var3 = ((Child)var1).getFirstWithoutReference(); var3 != null; var3 = ((Child)var3).getNextWithoutReference()) {
- if (var2.match(var3)) {
- var0.addElement(var3);
- }
-
- if (var3 instanceof Element) {
- addMatchingNodes(var0, (Element)var3, var2);
- }
- }
-
- }
-
- private static RelTerm makeRelTerm(Child var0) throws LibraryException, IllegalArgumentException {
- Parent var1 = (Parent)var0.getParentWithoutReference();
- short var2 = var0.getNodeType();
- int var3 = 1;
- switch (var2) {
- case 1:
- String var4 = var0.getNodeName();
-
- for(Node var8 = var1.getFirstWithoutReference(); var8 != null; var8 = ((Child)var8).getNextWithoutReference()) {
- if (var8.getNodeType() == var2 && var8.getNodeName().equals(var4)) {
- if (var8 == var0) {
- return new RelTerm(-1, var3, var4);
- }
-
- ++var3;
- }
- }
- break;
- case 2:
- case 5:
- case 6:
- default:
- throw new IllegalArgumentException("com.ibm.xml.xpointer.XPointer#makeRelTerm():");
- case 3:
- for(Node var7 = var1.getFirstWithoutReference(); var7 != null; var7 = ((Child)var7).getNextWithoutReference()) {
- if (var7.getNodeType() == var2) {
- if (var7 == var0) {
- return new RelTerm(-1, var3, 5);
- }
-
- ++var3;
- }
- }
- break;
- case 4:
- for(Node var6 = var1.getFirstWithoutReference(); var6 != null; var6 = ((Child)var6).getNextWithoutReference()) {
- if (var6.getNodeType() == var2) {
- if (var6 == var0) {
- return new RelTerm(-1, var3, 6);
- }
-
- ++var3;
- }
- }
- break;
- case 7:
- case 8:
- for(Node var5 = var1.getFirstWithoutReference(); var5 != null; var5 = ((Child)var5).getNextWithoutReference()) {
- if (var5.getNodeType() == var2) {
- if (var5 == var0) {
- return new RelTerm(-1, var3, var2 == 8 ? 4 : 3);
- }
-
- ++var3;
- }
- }
- }
-
- throw new LibraryException("com.ibm.xml.xpointer.XPointer#makeRelTerm(): Specified child isn't a child of specified element.");
- }
- }
-